from multiprocessing import Pool

from utils import fsutils
from workflows import w02_single_table_image_to_boxes
from workflows.w00_picture_trimming_correction import w00_picture_trimming_correction
from workflows.w01_convert_image_to_tables import w01_convert_image_to_tables
from workflows.w03_boxes_to_csv import w03_boxes_to_csv


def app_workflow():
    print()
    # # 获取w00步的路径列表（原始图片）
    # w00_image_paths = fsutils.get_filepaths(path="原始图片")
    # # 创建一个进程池来进行图切边矫正
    # with Pool() as pool:
    #     pool.map(w00_picture_trimming_correction, w00_image_paths)

    # 获取w01步的路径列表（矫正后的图片）
    w01_image_paths = fsutils.get_filepaths(fsutils.get_cache_path_for_category("矫正后的图片"))
    # 创建一个进程池处理图片分隔和图片黑白化
    with Pool() as pool:
        pool.map(w01_convert_image_to_tables, w01_image_paths)

    # 获取处理后单个表格的文件路径，并进行OCR识别
    single_table_paths = fsutils.get_filepaths(".cache/分隔后的表图片")
    with Pool() as pool:
        pool.map(w02_single_table_image_to_boxes, single_table_paths)

    # 获取OCR识别结果，并将其转换为CSV格式
    ocr_results_paths = fsutils.get_cache_files_in_category("ocr_converted_result")
    with Pool() as pool:
        pool.map(w03_boxes_to_csv, ocr_results_paths)


if __name__ == '__main__':
    app_workflow()
